package com.cinc.mdm.modules.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.mdm.constant.MdmConstant;
import com.cinc.mdm.modules.material.entity.MdmMaterial;
import com.cinc.mdm.modules.material.entity.MdmMaterialFactoryDetail;
import com.cinc.mdm.modules.material.entity.MdmMaterialFinanceDetail;
import com.cinc.mdm.modules.material.entity.MdmMaterialUnitDetail;
import com.cinc.mdm.modules.material.mapper.MdmMaterialFactoryDetailMapper;
import com.cinc.mdm.modules.material.mapper.MdmMaterialFinanceDetailMapper;
import com.cinc.mdm.modules.material.mapper.MdmMaterialMapper;
import com.cinc.mdm.modules.material.mapper.MdmMaterialUnitDetailMapper;
import com.cinc.mdm.modules.material.service.IMdmMaterialService;
import com.cinc.mdm.modules.materialClass.entity.MdmMaterialClass;
import com.cinc.mdm.modules.materialClass.mapper.MdmMaterialClassMapper;
import com.cinc.mdm.modules.materialCodeRule.service.IMdmMaterialCodeRuleService;
import com.cinc.mdm.modules.materialType.entity.MdmMaterialType;
import com.cinc.mdm.modules.materialType.mapper.MdmMaterialTypeMapper;
import com.cinc.mdm.modules.sysParam.entity.MdmSysParam;
import com.cinc.mdm.modules.sysParam.service.IMdmSysParamService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: mdm_material
 * @Author: jeecg-boot
 * @Date:   2021-10-13
 * @Version: V1.0
 */
@Service
public class MdmMaterialServiceImpl extends ServiceImpl<MdmMaterialMapper, MdmMaterial> implements IMdmMaterialService {

	@Autowired
	private MdmMaterialMapper mdmMaterialMapper;
	@Autowired
	private MdmMaterialFactoryDetailMapper mdmMaterialFactoryDetailMapper;
	@Autowired
	private MdmMaterialFinanceDetailMapper mdmMaterialFinanceDetailMapper;
	@Autowired
	private MdmMaterialUnitDetailMapper mdmMaterialUnitDetailMapper;
	@Autowired
	private IMdmMaterialCodeRuleService mdmMaterialCodeRuleService;
	@Autowired
	private MdmMaterialTypeMapper mdmMaterialTypeMapper;
	@Autowired
	private MdmMaterialClassMapper mdmMaterialClassMapper;
	@Autowired
	private IMdmSysParamService mdmSysParamService;

	@Override
	@Transactional
	public void saveMain(MdmMaterial mdmMaterial, List<MdmMaterialFactoryDetail> mdmMaterialFactoryDetailList, List<MdmMaterialFinanceDetail> mdmMaterialFinanceDetailList, List<MdmMaterialUnitDetail> mdmMaterialUnitDetailList) {
		// 自动编码
		LambdaQueryWrapper<MdmSysParam> sysParamQueryWrapper = new LambdaQueryWrapper<>();
		sysParamQueryWrapper.eq(MdmSysParam::getParamCode, MdmConstant.SYS_PARAM001)
				.eq(MdmSysParam::getEnableFlag, "1")
				.eq(MdmSysParam::getValue, "1");
		MdmSysParam mdmSysParam = mdmSysParamService.getOne(sysParamQueryWrapper);
		if(mdmSysParam != null){
			MdmMaterialType materialType = mdmMaterialTypeMapper.selectById(mdmMaterial.getMaterialTypeId());
			String materialTypeCode = materialType == null ? null : materialType.getMaterialTypeCode();
			MdmMaterialClass materialClass = mdmMaterialClassMapper.selectById(mdmMaterial.getMaterialClassId());
			String materialClassCode = materialClass == null ? null : materialClass.getMaterialClassCode();
			String materialCode = mdmMaterialCodeRuleService.generateMaterialCode(materialTypeCode, materialClassCode);
			mdmMaterial.setMaterialCode(materialCode);
		}
		if(oConvertUtils.isEmpty(mdmMaterial.getMaterialCode())){
			throw new RuntimeException("物料编号不能为空");
		}
		// 校验物料编号是否重复
		mdmMaterial.setMaterialCode(mdmMaterial.getMaterialCode());
		int count = mdmMaterialMapper.selectCount(new LambdaQueryWrapper<MdmMaterial>().eq(MdmMaterial::getMaterialCode, mdmMaterial.getMaterialCode()));
		if(count > 0){
			throw new RuntimeException("物料编号不能重复");
		}
		MdmMaterialType mdmMaterialType = mdmMaterialTypeMapper.selectById(mdmMaterial.getMaterialTypeId());
		mdmMaterial.setMaterialTypeCode(mdmMaterialType == null ? null : mdmMaterialType.getMaterialTypeCode());
		mdmMaterialMapper.insert(mdmMaterial);
		if(mdmMaterialFactoryDetailList!=null && mdmMaterialFactoryDetailList.size()>0) {
			for(MdmMaterialFactoryDetail entity:mdmMaterialFactoryDetailList) {
				//外键设置
				entity.setMaterialId(mdmMaterial.getId());
				mdmMaterialFactoryDetailMapper.insert(entity);
			}
		}
		if(mdmMaterialFinanceDetailList!=null && mdmMaterialFinanceDetailList.size()>0) {
			for(MdmMaterialFinanceDetail entity:mdmMaterialFinanceDetailList) {
				//外键设置
				entity.setMaterialId(mdmMaterial.getId());
				mdmMaterialFinanceDetailMapper.insert(entity);
			}
		}
		if(mdmMaterialUnitDetailList!=null && mdmMaterialUnitDetailList.size()>0) {
			for(MdmMaterialUnitDetail entity:mdmMaterialUnitDetailList) {
				//外键设置
				entity.setMaterialId(mdmMaterial.getId());
				mdmMaterialUnitDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(MdmMaterial mdmMaterial,List<MdmMaterialFactoryDetail> mdmMaterialFactoryDetailList,List<MdmMaterialFinanceDetail> mdmMaterialFinanceDetailList,List<MdmMaterialUnitDetail> mdmMaterialUnitDetailList) {
		if("".equals(mdmMaterial.getMaterialCode())){
			throw new RuntimeException("物料编码不能为空");
		}
		if("".equals(mdmMaterial.getMaterialName())){
			throw new RuntimeException("物料名称不能为空");
		}
		if(checkMaterialUsed(mdmMaterial.getId())){
			MdmMaterial originalMaterial = mdmMaterialMapper.selectById(mdmMaterial.getId());
			if(!originalMaterial.getMaterialCode().equals(mdmMaterial.getMaterialCode())){
				throw new RuntimeException("物料已经使用，不能修改物料编码");
			}
        }
		MdmMaterialType mdmMaterialType = mdmMaterialTypeMapper.selectById(mdmMaterial.getMaterialTypeId());
		mdmMaterial.setMaterialTypeCode(mdmMaterialType == null ? null : mdmMaterialType.getMaterialTypeCode());

		mdmMaterialMapper.updateById(mdmMaterial);

		//1.先删除子表数据
		mdmMaterialFactoryDetailMapper.deleteByMainId(mdmMaterial.getId());
		mdmMaterialFinanceDetailMapper.deleteByMainId(mdmMaterial.getId());
		mdmMaterialUnitDetailMapper.deleteByMainId(mdmMaterial.getId());

		//2.子表数据重新插入
		if(mdmMaterialFactoryDetailList!=null && mdmMaterialFactoryDetailList.size()>0) {
			for(MdmMaterialFactoryDetail entity:mdmMaterialFactoryDetailList) {
				//外键设置
				entity.setMaterialId(mdmMaterial.getId());
				mdmMaterialFactoryDetailMapper.insert(entity);
			}
		}
		if(mdmMaterialFinanceDetailList!=null && mdmMaterialFinanceDetailList.size()>0) {
			for(MdmMaterialFinanceDetail entity:mdmMaterialFinanceDetailList) {
				//外键设置
				entity.setMaterialId(mdmMaterial.getId());
				mdmMaterialFinanceDetailMapper.insert(entity);
			}
		}
		if(mdmMaterialUnitDetailList!=null && mdmMaterialUnitDetailList.size()>0) {
			for(MdmMaterialUnitDetail entity:mdmMaterialUnitDetailList) {
				//外键设置
				entity.setMaterialId(mdmMaterial.getId());
				mdmMaterialUnitDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		MdmMaterial originalMaterial = mdmMaterialMapper.selectById(id);
		if(checkMaterialUsed(originalMaterial.getId())){
			throw new RuntimeException("物料已经使用，不能删除");
		}
		mdmMaterialFactoryDetailMapper.deleteByMainId(id);
		mdmMaterialFinanceDetailMapper.deleteByMainId(id);
		mdmMaterialUnitDetailMapper.deleteByMainId(id);
		mdmMaterialMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			MdmMaterial originalMaterial = mdmMaterialMapper.selectById(id);
			if(checkMaterialUsed(originalMaterial.getId())){
				throw new RuntimeException("物料已经使用，不能删除");
			}
			mdmMaterialFactoryDetailMapper.deleteByMainId(id.toString());
			mdmMaterialFinanceDetailMapper.deleteByMainId(id.toString());
			mdmMaterialUnitDetailMapper.deleteByMainId(id.toString());
			mdmMaterialMapper.deleteById(id);
		}
	}

	/**
	 * 根据物料编码批量查询
	 */
	@Override
	public List<String> findMaterialCodeExists (List<String> materialCodeList){
		if(oConvertUtils.listIsEmpty(materialCodeList)){
			return null;
		}

		return mdmMaterialMapper.selectMaterialCodeExists(materialCodeList);
	}

	@Override
	public boolean checkMaterialUsed(String materialId) {
		int count = mdmMaterialMapper.selectUseCount(materialId);
		if(count > 0){
			return true;
		}
		return false;
	}

}
